import os
import sys
import django

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from apps.order.models import PriceUpdate
from apps.product.models import Product
from apps.sales_man.models import ChannelProduct
from core.tools.mysql_connect import SqlModel


class UpdatePrice(SqlModel):
    def __init__(self):
        super(UpdatePrice, self).__init__(database="default_online")

    def get_online_data(self):
        sql = "SELECT * FROM `order_priceupdate` ORDER BY `id` DESC  "
        self.cursor.execute(sql)
        results = self.cursor.fetchall()  # 获取查询的所有记录
        for i in results:
            print(i)
            channel_product_id = i[6]
            price_obj = PriceUpdate.objects.get(id=i[0])
            price_obj.channel_product_id = channel_product_id
            price_obj.save()

            # channel_product_obj = ChannelProduct.objects.get(id=pk)
            # channel_product_obj.
            #
            # product_id = i[6]
            # channel_product_obj = ChannelProduct.objects.get(product_id=product_id, channel_id=1)
            # channel_product_id = channel_product_obj.id
            # price_obj = PriceUpdate.objects.get(id=i[0])
            # price_obj.channel_product_id = channel_product_id
            # price_obj.save()


def update_price():
    # product_list = Product.objects.filter(name__contains="华夏").exclude(name="上海银行下线")
    product_list = Product.objects.filter(name__contains="华夏").exclude(name__contains="下线")
    for i in product_list:
        # print(i.name)
        channel_product = ChannelProduct.objects.get(channel_id=1, product=i)
        PriceUpdate.objects.create(
            channel_product=channel_product,
            price=45,
            leader_price=11.25,
            leader_leader_price=3.75,
        )


if __name__ == '__main__':
    # UpdatePrice().get_online_data()
    update_price()
